package com.platform.module.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.constant.UserConstants;
import com.platform.common.core.text.Convert;
import com.platform.common.exception.BusinessException;
import com.platform.common.utils.DateUtils;
import com.platform.common.utils.StringUtils;
import com.platform.common.utils.redis.RedisUtil;
import com.platform.module.domain.BaseConcentratorData;
import com.platform.module.domain.BaseConcentratorType;
import com.platform.module.domain.BaseFactory;
import com.platform.module.mapper.BaseConcentratorDataMapper;
import com.platform.module.mapper.BaseConcentratorTypeMapper;
import com.platform.module.mapper.BaseFactoryMapper;
import com.platform.module.service.IBaseConcentratorTypeService;
import com.platform.system.mapper.SysDictDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * 集中器型号数据Service业务层处理
 *
 * @author wangqi
 * @date 2020-07-13
 */
@Service
public class BaseConcentratorTypeServiceImpl extends ServiceImpl<BaseConcentratorTypeMapper, BaseConcentratorType> implements IBaseConcentratorTypeService
{
    @Autowired
    private BaseConcentratorTypeMapper baseConcentratorTypeMapper;

    @Autowired
    private BaseFactoryMapper baseFactoryMapper;

    @Autowired
    private BaseConcentratorDataMapper baseConcentratorDataMapper;

    /**
     * 查询集中器型号数据列表
     *
     * @param baseConcentratorType 集中器型号数据
     * @return 集中器型号数据
     */
    @Override
    public List<BaseConcentratorType> selectBaseConcentratorTypeList(BaseConcentratorType baseConcentratorType)
    {
        return baseConcentratorTypeMapper.selectBaseConcentratorTypeList(baseConcentratorType);
    }

    /**
     * 新增集中器型号数据
     *
     * @param baseConcentratorType 集中器型号数据
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBaseConcentratorType(BaseConcentratorType baseConcentratorType)
    {
        baseConcentratorType.setCreateTime(DateUtils.getNowDate());
        return baseConcentratorTypeMapper.insert(baseConcentratorType);
    }

    /**
     * 修改集中器型号数据
     *
     * @param baseConcentratorType 集中器型号数据
     * @return 结果
     */
    @Override
    @Transactional
    public int updateBaseConcentratorType(BaseConcentratorType baseConcentratorType)
    {
        baseConcentratorType.setUpdateTime(DateUtils.getNowDate());
        return baseConcentratorTypeMapper.updateById(baseConcentratorType);
    }

    /**
     * 删除集中器型号数据对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteBaseConcentratorTypeByIds(String ids)
    {
        return baseConcentratorTypeMapper.deleteBatchIds(Arrays.asList(Convert.toStrArray(ids)));
    }
    /**
     * 查询子设备数量
     *
     * @param typeId 设备类型
     * @return 结果
     */
    @Override
    public int selectCountConcentratorDataByTypeId(Long typeId)
    {
        return baseConcentratorDataMapper.selectCount(new QueryWrapper<BaseConcentratorData>().eq("type_id",typeId));
    }

    /**
     * 删除热量表型号数据对象
     *
     * @param typeId 需要删除的数据ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteBaseConcentratorTypeByTypeIds(Long typeId)
    {
        return baseConcentratorTypeMapper.deleteById(typeId);
    }




    /**
     * 导入集中器类型信息数据
     *
     * @param concentratorTypeList 集中器类型信息数据
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    @Transactional
    public String importBaseConcentratorType(List<BaseConcentratorType> concentratorTypeList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(concentratorTypeList) || concentratorTypeList.size() == 0)
        {
            throw new BusinessException("导入集中器类型信息数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (BaseConcentratorType concentratorType : concentratorTypeList)
        {
            try
            {
                //根据厂商名字查询厂商信息
                QueryWrapper<BaseFactory> factoryQueryWrapper = new QueryWrapper<BaseFactory>();
                factoryQueryWrapper.eq("factory_name",concentratorType.getFactoryName());
                BaseFactory baseFactory = baseFactoryMapper.selectOne(factoryQueryWrapper);
                if(baseFactory!=null){
                    concentratorType.setFactoryId(baseFactory.getFactoryId());
                }

                //查询集中器类型是否存在
                QueryWrapper<BaseConcentratorType> typeQueryWrapper = new QueryWrapper<BaseConcentratorType>();
                typeQueryWrapper.eq("type_name",concentratorType.getTypeName());
                BaseConcentratorType concentrator = baseConcentratorTypeMapper.selectOne(typeQueryWrapper);
                if (StringUtils.isNull(concentrator))
                {
                    concentratorType.setCreateBy(operName);
                    concentratorType.setCreateTime(DateUtils.getNowDate());
                    this.insertBaseConcentratorType(concentratorType);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、集中器类型 " + concentratorType.getTypeName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    concentratorType.setUpdateBy(operName);
                    concentratorType.setUpdateTime(DateUtils.getNowDate());
                    this.updateBaseConcentratorType(concentratorType);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、集中器类型 " + concentratorType.getTypeName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、集中器类型 " + concentratorType.getTypeName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、集中器类型 " + concentratorType.getTypeName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 校验厂商代码是否唯一
     *
     * @param baseConcentratorType 厂商信息
     * @return 结果
     */
    @Override
    public String checkConcentratorTypeCodeUnique(BaseConcentratorType baseConcentratorType)
    {
        Long typeId = StringUtils.isNull(baseConcentratorType.getTypeId()) ? -1L : baseConcentratorType.getTypeId();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("type_name",baseConcentratorType.getTypeName());
        BaseConcentratorType concentratorType = baseConcentratorTypeMapper.selectOne(queryWrapper);
        if (StringUtils.isNotNull(concentratorType) && concentratorType.getTypeId().longValue() != typeId.longValue())
        {
            return UserConstants.CONCENTRATOR_TYPE_NOT_UNIQUE;
        }
        return UserConstants.CONCENTRATOR_TYPE_UNIQUE;
    }
}
